<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body{
      margin: 0;
      padding: 0;
      background: url(./grid.png);
    }
    #canvas{
      background-color: rgb(255, 192, 203, 0.4);
    }
  </style>
</head>
<body>
  <canvas id="canvas" width="500px" height="500px"></canvas>
  <script>
    window.onload = function(){
      const canvas = document.getElementById('canvas')
      if(canvas.getContext){
        const ctx = canvas?.getContext('2d')

        requestAnimationFrame(draw)

        let count = 0
        function draw(){
          let second = new Date().getSeconds()
          let mm = new Date().getMilliseconds()
          console.log('second: ', second, mm);
          // 1.清空画布
          ctx.clearRect(0, 0, 500, 500)
          // 2.保存画布原始状态
          ctx.save()
          // 3.形变
          ctx.translate(250, 250)
          ctx.rotate(Math.PI / 30 * second + Math.PI / 30000 * mm) // canvas旋转的是弧度  1s相当于旋转1°
          // 4.绘图
          ctx.beginPath()
          ctx.moveTo(0, 0)
          ctx.lineTo(0, -250)
          ctx.closePath()
          ctx.stroke()
          // 5.恢复画布原始状态
          ctx.restore()
          requestAnimationFrame(draw)
        }
      }
    }
  </script>
</body>
</html>